<?php

namespace V1\Model;


use Think\Exception;
use Think\Model;

class AskModel extends Model
{
    protected $tableName = 'question_ask';

    /**
     * 添加提问
     * @param $info
     * @return mixed
     */
    public function add_quick_ask($info)
    {
        $data = [
            'uid' => $info['uid'],
            'content' => urldecode($info['content']),
            'thumb0' => !empty($info['thumb0']) ? $info['thumb0'] : '',
            'thumb1' => !empty($info['thumb1']) ? $info['thumb1'] : '',
            'thumb2' => !empty($info['thumb2']) ? $info['thumb2'] : '',
            'thumb3' => !empty($info['thumb3']) ? $info['thumb3'] : '',
            'thumb4' => !empty($info['thumb4']) ? $info['thumb4'] : '',
            'thumb5' => !empty($info['thumb5']) ? $info['thumb5'] : '',
            'addtime' => time(),
            'catid' => $info['cat_id'],
            'gold' => $info['gold']
        ];
        return $this->add($data);
    }


    /**
     * 获取内容详情
     * @param int $askid
     * @param null $userid
     * @return mixed
     */
    public function get_problem_details($askid, $userid = null)
    {
        $map['id'] = $askid;
        $question_ask_model = M('question_ask');
        $integral_model = new IntegralModel();
        $question_adopt_model = new QuestionAdoptModel();
        $data = $question_ask_model->alias('ask')->join('LEFT JOIN destoon_ucenter_member m_member ON ask.uid = m_member.userid')
            ->join('LEFT JOIN __MEMBER_PROFILE__ profile ON ask.uid = profile.userid')
            ->field('ask.id,ask.uid,ask.content,ask.addtime,ask.thumb0,ask.thumb1,ask.thumb2,ask.thumb3,ask.thumb4,ask.thumb5,ask.perfect_answer_id,ask.areaid,ask.catid,ask.answer_number,ask.gold,profile.truename,profile.areaid,profile.address,profile.avatar,profile.memberships')
            ->where($map)
            ->select();
        foreach ($data as $k => $v) {
            $member_profile = new MemberProfileModel();
            $member = $member_profile->get_user_details_ask_list($v['uid']);
            //积分等级
            $data[$k]['grade'] = $integral_model->grade_type($member['profile'][0]['member_profile']['score'], $v['uid']);
            //勋章
            $request[$k]['memberships'] = $member['profile'][0]['member_profile']['memberships'];
            $data[$k]['medal'] = $member['medal'];
            $adopt_data = $question_adopt_model->get_answer_adopt_status($askid);                 //获取问题是否采纳有答案
            if (count($adopt_data) > 0) {
                $data[$k]['adopt'] = true;
            } else {
                $data[$k]['adopt'] = false;
            }
        }
        return $data;
    }

    /**
     * 统计回复消息
     */
    public function get_ask_number($id)
    {
        $map['id'] = $id;
        $request = $this->where($map)->setInc('answer_number', 1);
        return $request;
    }

    /**
     * 通过问题id获取用户id
     * @param int $id 问题id
     * @return bool|mixed
     */
    public function get_uid($id)
    {
        $map['id'] = $id;
        $request = $this->field('348')->where($map)->find();
        return !empty($request) ? $request : false;
    }


    /**
     * 提问历史
     * @param int $uid
     * @return mixed
     */
    public function get_ask_history($uid)
    {
        $map['uid'] = $uid;
            $request = $this->where($map)->order('addtime desc')->select();
            if($request===false){
                new Exception();
            }else{
                foreach ($request as $k => $v) {
                    $request[$k]['addtime'] = date("Y-m-d", $v['addtime']);
                }
            }
        return $request;
    }
}